package com.cpdmali.api.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateHelper {
	
    private static final Logger LOG = LoggerFactory.getLogger(DateHelper.class);
	
	private static final String format1 = "dd/MM/yy HH:mm:ss,SSSSSSSSS";
	
	private static final String format2 = "yyyy-MM-dd HH:mm:ss";
	
	private static final String format3 = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

	/**
	 * Gets a Date from a String in the tables of Nuxeo.
	 * 
	 * @param data
	 *            the date in String format format
	 * @return the date converted from the String
	 */
	public static Date fromStringNuxeoDatabaseToDate(final String data) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format1);
		Date date = null;
		try {
			date = simpleDateFormat.parse(data);
		} catch (ParseException e) {
			LOG.warn("Error parsing the date with the format " + format1);
		}
		return date;
	}

	/**
	 * Gets a String from a Date in the tables of Nuxeo. The date in String format is format.
	 * 
	 * @param date
	 *            the date
	 * @return the date converted from to a String
	 */
	public static String toStringFromDate(final Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format1);
		String data = null;
		if (date != null) {
			data = simpleDateFormat.format(date);
		}
		return data;
	}
	
	/**
	 * Gets a String from a Date in a format3.
	 * 
	 * @param date
	 *            the date
	 * @return the date converted from to a String
	 */
	public static String toIso8601StringFromDate(final Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format3);
		String data = null;
		if (date != null) {
			data = simpleDateFormat.format(date);
		}
		return data;
	}

	/**
	 * Gets a String in format format corresponding to the current date.
	 * 
	 * @return the date converted from to a String
	 */
	public static String getStringDateCorrespondingToNow() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format1);
		String data = null;
		data = simpleDateFormat.format(Calendar.getInstance().getTime());
		return data;
	}

	/**
	 * Create a date from a gregorian calendar.
	 * 
	 * @param gregorianCalendar
	 *            gregorianCalendar
	 * @return a date
	 */
	public static Date fromGregorianCalendarToDate(final GregorianCalendar gregorianCalendar) {
		Date date = null;
		if (gregorianCalendar != null) {
			date = gregorianCalendar.getTime();
		}
		return date;
	}
	
	
	/**
	 * Create a date from an XML gregorian calendar.
	 * 
	 * @param xmlGregorianCalendar
	 *            xml gregorianCalendar
	 * @return a date
	 */
	public static Date fromXMLGregorianCalendarToDate(final XMLGregorianCalendar xmlGregorianCalendar) {
		Date date = null;
		if (xmlGregorianCalendar != null) {
			date = xmlGregorianCalendar.toGregorianCalendar().getTime();
		}
		return date;
	}
	
	/**
	 * Create a date from a gregorian calendar.
	 * 
	 * @param gregorianCalendar
	 *            gregorianCalendar
	 * @return a date
	 */
	public static Calendar fromDateToCalendar(final Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}


	

	
	
	/**
	 * Convert a GregorianCalendar date to an XMLGregorianCalendar one.
	 * @param date
	 * @return XMLGregorianCalendar
	 * @throws DatatypeConfigurationException
	 */
	public static XMLGregorianCalendar toXMLGregorianCalendarfromGregorianDate(GregorianCalendar date) throws DatatypeConfigurationException {
		XMLGregorianCalendar returnDate = null;
		if (date != null) {
			returnDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(date);
		} 
		return returnDate;
	}
	
	/**
	 * Convert a Date to an XMLGregorianCalendar.
	 * @param date
	 * @return XMLGregorianCalendar
	 * @throws DatatypeConfigurationException
	 */
	public static XMLGregorianCalendar toXMLGregorianCalendarfromDate(Date date) throws DatatypeConfigurationException {
		XMLGregorianCalendar returnDate = null;
		GregorianCalendar gDate = new GregorianCalendar();
		if (date != null) {
			gDate.setTime(date);
			returnDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gDate);
		} 
		return returnDate;
	}
}
